package tman.simulator.snapshot;

import common.peer.PeerAddress;
import java.util.ArrayList;

import se.sics.asdistances.ASDistances;


public class PeerInfo {
	private PeerAddress succ;
	private PeerAddress pred;
	private ArrayList<PeerAddress> tmanPartners;
	private ArrayList<PeerAddress> cyclonPartners;
	private ArrayList<PeerAddress> smallWorldPartners;

	//-------------------------------------------------------------------
	public PeerInfo() {
		this.tmanPartners = new ArrayList<PeerAddress>();
		this.cyclonPartners = new ArrayList<PeerAddress>();
	}

	//-------------------------------------------------------------------
	public void updateSuccPred(PeerAddress succ, PeerAddress pred) {
		this.succ = succ;
		this.pred = pred;
	}

	//-------------------------------------------------------------------
	public void updateTManPartners(ArrayList<PeerAddress> partners) {
		this.tmanPartners = partners;
	}

	//-------------------------------------------------------------------
	public void updateCyclonPartners(ArrayList<PeerAddress> partners) {
		this.cyclonPartners = partners;
	}

	public void updateSmallWorldPartners(ArrayList<PeerAddress> partners) {
		this.smallWorldPartners = partners;
	}

	//-------------------------------------------------------------------
	public PeerAddress getSucc() {
		return this.succ;
	}

	//-------------------------------------------------------------------
	public PeerAddress getPred() {
		return this.pred;
	}

	//-------------------------------------------------------------------
	public ArrayList<PeerAddress> getTManPartners() {
		return this.tmanPartners;
	}

	//-------------------------------------------------------------------
	public ArrayList<PeerAddress> getCyclonPartners() {
		return this.cyclonPartners;
	}

	public ArrayList<PeerAddress> getSmallWorldPartners() {
		return this.smallWorldPartners;
	}

	public String displaySmallWorldPartners(PeerAddress peer) {
		if(smallWorldPartners != null) {
			String str = "[";
			float avg = 0;
			ASDistances d = ASDistances.getInstance();
			for(int c = 0; c < smallWorldPartners.size(); c++) {
				int i = d.getDistance(peer.getPeerAddress().getIp().getHostAddress(), smallWorldPartners.get(c).getPeerAddress().getIp().getHostAddress());
				str += "("+ smallWorldPartners.get(c) +", "+ i +"), ";
				avg = ((avg*c) + i)/(c+1);
			}
			return str.substring(0, str.length()-2) + "] Avg ASDistance: " + avg;
		} else {
			return "[]";
		}
	}
}
